#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "ChineseEnglishMapping.h"
#include "TableWidgetForm.h"
#include "350/Alarm.h"
#include "350/AutoControl.h"
#include "350/CustomMoto.h"
#include "350/CustomTempDiff.h"
#include "350/Fan.h"
#include "350/GlobalParam.h"
#include "350/IO.h"
#include "350/MainPower.h"
#include "350/Monitor.h"
#include "350/Moto.h"
#include "350/SmokeSensor.h"
#include "350/TempSensor350.h"
#include "350/user.h"
#include "1021/TempSensor1021.h"
#include "1028/AtDevice.h"
#include "1028/AutoControl1028.h"
#include "1028/TempSensor1028.h"
#include <QSplitter>
#include <QSettings>
#include <QDir>
#include <QDebug>
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ReadBIConfig();
    InitUI();
    InitListWidget();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::SlotItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
{
    for (auto item : m_pTableWidgetFormMap.values())
    {
        for (auto widget : item) {
            delete widget;
            widget = nullptr;
        }
    }
    m_pTableWidgetFormMap.clear();
    ui->tabWidget->clear();

    QList<QPair<QWidget *, QString>> widgetList;
    QMap<int, QList<TableWidgetForm*>> TableWidgetFormMap;

    QString fileName = current->data(Qt::UserRole).toString();
    if(COMMONS->MachType == "350")
    {
        if(fileName == "Alarm.xml")
            Alarm::InitUI(fileName, widgetList, TableWidgetFormMap);
        else if(fileName == "AutoControl.xml")
            AutoControl::InitUI(fileName, widgetList, TableWidgetFormMap);
        else if(fileName == "CustomMoto-350.xml")
            CustomMoto::InitUI(fileName, widgetList, TableWidgetFormMap);
        else if(fileName == "CustomTempDiff.xml")
            CustomTempDiff::InitUI(fileName,widgetList,TableWidgetFormMap);
        else if(fileName == "Fan.xml")
            Fan::InitUI(fileName,widgetList,TableWidgetFormMap);
        else if(fileName == "GlobalParam.xml")
            GlobalParam::InitUI(fileName,widgetList,TableWidgetFormMap);
        else if(fileName == "IO.xml")
            IO::InitUI(fileName,widgetList,TableWidgetFormMap);
        else if(fileName == "MainPower.xml")
            MainPower::InitUI(fileName,widgetList,TableWidgetFormMap);
        else if(fileName == "Monitor.xml")
            Monitor::InitUI(fileName,widgetList,TableWidgetFormMap);
        else if(fileName == "Moto.xml")
            Moto::InitUI(fileName,widgetList,TableWidgetFormMap);
        else if(fileName == "SmokeSensor.xml")
            SmokeSensor::InitUI(fileName,widgetList,TableWidgetFormMap);
        else if(fileName == "TempSensor.xml")
            TempSensor350::InitUI(fileName,widgetList,TableWidgetFormMap);
        else if(fileName == "user.xml")
            user::InitUI(fileName,widgetList,TableWidgetFormMap);
    }
    else if(COMMONS->MachType  == "1021")
    {
        if(fileName == "TempSensor.xml")
            TempSensor1021::InitUI(fileName,widgetList,TableWidgetFormMap);
    }
    else if(COMMONS->MachType  == "1028")
    {
        if(fileName == "AutoControl.xml")
            AutoControl1028::InitUI(fileName,widgetList,TableWidgetFormMap);
        else if(fileName == "TempSensor.xml")
            TempSensor1028::InitUI(fileName,widgetList,TableWidgetFormMap);
        else if(fileName == "AtDevice.xml")
            AtDevice::InitUI(fileName,widgetList,TableWidgetFormMap);
    }

    for (auto item : widgetList)
    {
        ui->tabWidget->addTab(item.first,item.second);
    }
    m_pTableWidgetFormMap = TableWidgetFormMap;
}

void MainWindow::SlotBtnSave()
{
    int ret = QMessageBox::question(this,QStringLiteral("提示"),QStringLiteral("是否保存？"),QMessageBox::Ok|QMessageBox::Cancel);
    if(ret == QMessageBox::Ok)
    {
        QListWidgetItem* item = ui->listWidget->currentItem();
        if(item)
        {
            bool isflag = false;
            QString fileName = item->data(Qt::UserRole).toString();
            if(COMMONS->MachType == "350")
            {
                if(fileName == "Alarm.xml")
                    isflag = Alarm::SaveUI(fileName, m_pTableWidgetFormMap);
                else if(fileName == "AutoControl.xml")
                    isflag = AutoControl::SaveUI(fileName, m_pTableWidgetFormMap);
                else if(fileName == "CustomMoto-350.xml")
                    isflag = CustomMoto::SaveUI(fileName, m_pTableWidgetFormMap);
                else if(fileName == "CustomTempDiff.xml")
                    isflag = CustomTempDiff::SaveUI(fileName, m_pTableWidgetFormMap);
                else if(fileName == "Fan.xml")
                    isflag = Fan::SaveUI(fileName, m_pTableWidgetFormMap);
                else if(fileName == "GlobalParam.xml")
                    isflag = GlobalParam::SaveUI(fileName, m_pTableWidgetFormMap);
                else if(fileName == "IO.xml")
                    isflag = IO::SaveUI(fileName, m_pTableWidgetFormMap);
                else if(fileName == "MainPower.xml")
                    isflag = MainPower::SaveUI(fileName, m_pTableWidgetFormMap);
                else if(fileName == "Monitor.xml")
                    isflag = Monitor::SaveUI(fileName, m_pTableWidgetFormMap);
                else if(fileName == "Moto.xml")
                    isflag = Moto::SaveUI(fileName, m_pTableWidgetFormMap);
                else if(fileName == "SmokeSensor.xml")
                    isflag = SmokeSensor::SaveUI(fileName, m_pTableWidgetFormMap);
                else if(fileName == "TempSensor.xml")
                    isflag = TempSensor350::SaveUI(fileName, m_pTableWidgetFormMap);
                else if(fileName == "user.xml")
                    isflag = user::SaveUI(fileName, m_pTableWidgetFormMap);
            }
            else if(COMMONS->MachType  == "1021")
            {
                if(fileName == "TempSensor.xml")
                    isflag = TempSensor1021::SaveUI(fileName, m_pTableWidgetFormMap);
            }
            else if(COMMONS->MachType  == "1028")
            {
                if(fileName == "AutoControl.xml")
                    isflag = AutoControl1028::SaveUI(fileName, m_pTableWidgetFormMap);
                else if(fileName == "TempSensor.xml")
                    isflag = TempSensor1028::SaveUI(fileName, m_pTableWidgetFormMap);
                else if(fileName == "AtDevice.xml")
                    isflag = AtDevice::SaveUI(fileName, m_pTableWidgetFormMap);
            }

            if(isflag)
            {
                QMessageBox::information(this,QStringLiteral("提示"),QStringLiteral("保存成功！"));
            }
            else {
                QMessageBox::information(this,QStringLiteral("提示"),QStringLiteral("错误！"));
            }
        }
    }
}

void MainWindow::SlotBtnExit()
{
    exit(0);
}

void MainWindow::InitUI()
{
    //设置大小
    ui->listWidget->setMinimumWidth(250);
    ui->tabWidget->setMinimumWidth(600);
    //失能最小化子窗口
    ui->splitter->setChildrenCollapsible(false);
    ui->splitter->setStretchFactor(0, 1);
    ui->splitter->setStretchFactor(1, 9);

    connect(ui->listWidget,&QListWidget::currentItemChanged,this,&MainWindow::SlotItemChanged);
    connect(ui->btn_Save,&QPushButton::clicked,this,&MainWindow::SlotBtnSave);
    connect(ui->btn_Exit,&QPushButton::clicked,this,&MainWindow::SlotBtnExit);
}

void MainWindow::InitListWidget()
{
    QString directoryPath = QString("%1/config/%2").arg(COMMONS->XmlPath).arg(COMMONS->MachType);
    QDir dir(directoryPath);
    // 设置文件过滤器，获取所有的 .xml 文件
    dir.setNameFilters({"*.xml"});
    dir.setFilter(QDir::Files); // 仅返回文件，不返回目录
    // 获取目录下的所有 .xml 文件
    QStringList xmlFiles = dir.entryList();
    for (auto xmlFile : xmlFiles)
    {
        QListWidgetItem* ListWidgetItem = new QListWidgetItem(QIcon(":/image/setting.ico"), xmlFile);
        ListWidgetItem->setData(Qt::UserRole, COMMONS->getListWidgetToTitle(COMMONS->MachType, xmlFile).toString());
        ui->listWidget->addItem(ListWidgetItem);
    }
    if(ui->listWidget->count() > 0)
    {
        ui->listWidget->setCurrentRow(0);
    }
}

void MainWindow::ReadBIConfig()
{
    QString filepath = COMMONS->XmlPath+"/config/config.ini";
    QSettings setting(filepath, QSettings::IniFormat);
    setting.beginGroup("MachineType");
    COMMONS->MachType = setting.value("Type").toString();
    setting.endGroup();
    //设置标题
    setWindowTitle(QStringLiteral("BIConfigTool-%1").arg(COMMONS->getModelNameToTitle(COMMONS->MachType).toString()));
}
